﻿using AutoMapper;

namespace Journeo_backend.Services.Implementations
{
    public class TransportationPlanService : ITransportationPlanService
    {
        private readonly IMapper _mapper;
        private readonly ApplicationDbContext _context;
        public TransportationPlanService(IMapper mapper, ApplicationDbContext context)
        {
            _mapper = mapper;
            _context = context;
        }
        // 创建交通计划
        public async Task<Response<TransportationPlan>> CreateTransportationPlan(TransportationPlanDTO transportationPlanDTO)
        {
            TransportationPlan transportationPlan = _mapper.Map<TransportationPlan>(transportationPlanDTO);
            _context.TransportationPlan.Add(transportationPlan);

            try
            {
                await _context.SaveChangesAsync();
                return new Response<TransportationPlan>(transportationPlan);
            }
            catch (Exception e)
            {
                return new Response<TransportationPlan>("error", e.Message);
            }
        }

        // 更新交通计划
        public async Task<Response<TransportationPlan>> UpdateTransportationPlan(long transportId, TransportationPlanDTO transportationPlanDTO)
        {
            var transportationPlan = _context.TransportationPlan.FirstOrDefault(p => p.TransportId == transportId);
            transportationPlan = _mapper.Map(transportationPlanDTO, transportationPlan);

            try
            {
                await _context.SaveChangesAsync();
                return new Response<TransportationPlan>(transportationPlan);
            }
            catch (Exception e)
            {
                return new Response<TransportationPlan>("error", e.Message);
            }
        }

        // 删除交通计划
        public async Task<Response<string>> DeleteTransportationPlan(long transportId)
        {
            try
            {
                TransportationPlan transportationPlan = await _context.TransportationPlan.FirstOrDefaultAsync(t => t.TransportId == transportId);
                _context.TransportationPlan.Remove(transportationPlan);
                await _context.SaveChangesAsync();
                return new Response<string>(Messages.SUCCESS);
            }
            catch (Exception e)
            {
                return new Response<string>("error", e.Message);
            }
        }

        // 删除计划的所有交通计划
        public async Task<Response<string>> DeleteTransportationPlans(long planId)
        {
            try
            {
                var transportations = _context.TransportationPlan.Where(t => t.PlanId == planId).ToList();
                if (transportations.Any())
                {
                    _context.RemoveRange(transportations);
                    await _context.SaveChangesAsync();
                }
                return new Response<string>(Messages.SUCCESS);
            }
            catch (Exception e)
            {
                return new Response<string>("error", e.Message);
            }
        }

        // 根据交通计划Id获取交通计划
        public async Task<Response<TransportationPlan>> GetTransportationPlan(long transportId)
        {
            try
            {
                TransportationPlan transportationPlan = await _context.TransportationPlan.FindAsync(transportId);
                return new Response<TransportationPlan>(transportationPlan);
            }
            catch (Exception e)
            {
                return new Response<TransportationPlan>("error", e.Message);
            }
        }

        // 获取计划的所有交通计划
        public async Task<PageResponse<TransportationPlan>> GetTransportationPlans(long planId, int pageNumber, int pageSize)
        {
            try
            {
                // 根据planId进行搜索
                var query = _context.TransportationPlan.Where(t => t.PlanId == planId);
                var totalCount = query.Count();
                var transportationPlans = query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
                return await Task.FromResult(new PageResponse<TransportationPlan>(transportationPlans.ToList(), pageNumber, pageSize, totalCount));
            }
            catch (Exception e)
            {
                return await Task.FromResult(new PageResponse<TransportationPlan>("error", e.Message, null, pageNumber, pageSize, 0));
            }
        }
    }
}
